<?php

/**
 * @Author: Wang chunsheng  email:2192138785@qq.com
 * @Date:   2023-04-22 13:26:52
 * @Last Modified by:   Wang chunsheng  email:2192138785@qq.com
 * @Last Modified time: 2023-05-07 12:00:22
 */

namespace Common\Models\sys;

use Common\BaseModel;
use Common\Models\Enums\DeviceTypeEnums;
use Common\Plugin\Interface\Doorlock\Models\DeviceDoorlock;
use Common\Plugin\Interface\Doorlock\Models\DeviceGateway;
use Common\Plugin\Interface\EnergyMeter\Models\EnergyModel;
use Common\Plugin\Interface\Receptacle\Models\DeviceReceptacle;
use Hyperf\Database\Model\Relations\HasOne;
use Hyperf\ModelCache\Cacheable;
use Hyperf\ModelCache\CacheableInterface;
use Hyperf\Tappable\HigherOrderTapProxy;

/**
 * @property HigherOrderTapProxy|mixed|null $type
 */
class SysDevice extends BaseModel implements CacheableInterface
{
    use Cacheable;

    protected ?string $table = 'sys_device_list';

    protected array $fillable = ['mac', 'device_mac', 'device_id', 'project_id', 'member_id', 'bloc_id', 'store_id', 'type', 'title', 'building_remark', 'floor_remark', 'unit_remark', 'room_remark', 'cate_pid', 'cate_id', 'desc', 'model_name', 'device_sn'];

//    public static array $rules = [
//        'titles' => 'required|notEmpty',
//    ];
//
//    // 验证错误消息提示
//    public static array $messages = [
//        'titles.required' => '名字不能为空！',
//    ];
//
//    // 验证字段的别名
//    public static array $alias = [
//        'titles' => '设备名称',
//    ];

    public function getSpecs(): \Hyperf\Database\Model\Relations\HasMany
    {
        return $this->hasMany(SysDeviceSpecs::class, null, 'device_id', 'id');
    }

    public function deviceStatus(): HasOne|array
    {
        switch ($this->type) {
            case DeviceTypeEnums::DeviceType48://零火开关
                return $this->hasOne(DeviceReceptacle::class, 'device_id', 'id');
            case DeviceTypeEnums::DeviceType90://智能电表
            case DeviceTypeEnums::DeviceType91://智能电表
                return $this->hasOne(EnergyModel::class, 'device_id', 'id');
            case DeviceTypeEnums::DeviceType81://智能门锁
                return $this->hasOne(DeviceDoorlock::class, 'device_id', 'id');
            case DeviceTypeEnums::DeviceType06://智能网关
                return $this->hasOne(DeviceGateway::class, 'device_id', 'id');
        }
        return [];
    }

    /**
     * 缓存 10 分钟，返回 null 则使用配置文件中设置的超时时间
     * @return int|null
     */
    public function getCacheTTL(): ?int
    {
        return 600;
    }
}
